package org.streets.eis.component.grid;


import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.model.IModel;

import com.inmethod.grid.column.AbstractColumn;
import org.streets.eis.component.ChangedNotifyType;
import org.streets.eis.component.codegroup.ListCodeGroup;
import org.streets.eis.component.codegroup.TreeCodeGroup;
import org.streets.eis.component.model.ListItem;

public class CodeGroupColumn extends EditableColumn {
	private static final long serialVersionUID = 1L;

	private ColumnType.CodeGroup codeGroupType = ColumnType.CodeGroup.LIST;

	private String codeGroupId = "";
	
	private String conditionSql = "";
	
	private boolean canInput;
	
	private ChangedNotifyType changedNotifyType = ChangedNotifyType.NONE;

	@SuppressWarnings("unchecked")	
	public CodeGroupColumn(String columnId, IModel headerModel, String propertyExpression, String sortProperty,ColumnType.CodeGroup codeGroupType,String codeGroupId) {
		this(columnId, headerModel, propertyExpression, sortProperty,codeGroupType,codeGroupId,true);
	}

	@SuppressWarnings("unchecked")	
	public CodeGroupColumn(String columnId, IModel headerModel, String propertyExpression, String sortProperty,ColumnType.CodeGroup codeGroupType,String codeGroupId,boolean canInput) {
		this(columnId, headerModel, propertyExpression, sortProperty,codeGroupType,codeGroupId,"",canInput);
	}
	
	@SuppressWarnings("unchecked")	
	public CodeGroupColumn(String columnId, IModel headerModel, String propertyExpression, String sortProperty,ColumnType.CodeGroup codeGroupType,String codeGroupId,String conditionSql) {
		this(columnId, headerModel, propertyExpression, sortProperty,codeGroupType,codeGroupId,conditionSql,true);
	}
	
	@SuppressWarnings("unchecked")	
	public CodeGroupColumn(String columnId, IModel headerModel, String propertyExpression, String sortProperty,ColumnType.CodeGroup codeGroupType,String codeGroupId,String conditionSql,boolean canInput) {
		super(columnId, headerModel, propertyExpression, sortProperty);
		this.codeGroupType = codeGroupType;
		this.codeGroupId = codeGroupId;
		this.conditionSql = conditionSql;
		this.canInput = canInput;
	}

	@SuppressWarnings("unchecked")	
	public CodeGroupColumn(String columnId, IModel headerModel, String propertyExpression,ColumnType.CodeGroup codeGroupType,String codeGroupId) {
		this(columnId, headerModel, propertyExpression,codeGroupType,codeGroupId,true);
	}
	
	@SuppressWarnings("unchecked")	
	public CodeGroupColumn(String columnId, IModel headerModel, String propertyExpression,ColumnType.CodeGroup codeGroupType,String codeGroupId,String conditionSql) {
		this(columnId, headerModel, propertyExpression,codeGroupType,codeGroupId,conditionSql,true);
	}

	@SuppressWarnings("unchecked")	
	public CodeGroupColumn(String columnId, IModel headerModel, String propertyExpression,ColumnType.CodeGroup codeGroupType,String codeGroupId,boolean canInput) {
		this(columnId, headerModel, propertyExpression,codeGroupType,codeGroupId,"",canInput);
	}
	
	@SuppressWarnings("unchecked")	
	public CodeGroupColumn(String columnId, IModel headerModel, String propertyExpression,ColumnType.CodeGroup codeGroupType,String codeGroupId,String conditionSql,boolean canInput) {
		super(columnId, headerModel, propertyExpression);
		this.codeGroupType = codeGroupType;
		this.codeGroupId = codeGroupId;
		this.conditionSql = conditionSql;
		this.canInput = canInput;
	}
	
	@SuppressWarnings("unchecked")	
	public CodeGroupColumn(IModel headerModel, String propertyExpression, String sortProperty,ColumnType.CodeGroup codeGroupType,String codeGroupId) {
		this(headerModel, propertyExpression, sortProperty,codeGroupType,codeGroupId,true);
	}
	
	@SuppressWarnings("unchecked")	
	public CodeGroupColumn(IModel headerModel, String propertyExpression, String sortProperty,ColumnType.CodeGroup codeGroupType,String codeGroupId,String conditionSql) {
		this(headerModel, propertyExpression, sortProperty,codeGroupType,codeGroupId,conditionSql,true);
	}
	
	@SuppressWarnings("unchecked")	
	public CodeGroupColumn(IModel headerModel, String propertyExpression, String sortProperty,ColumnType.CodeGroup codeGroupType,String codeGroupId,boolean canInput) {
		this(headerModel, propertyExpression, sortProperty,codeGroupType,codeGroupId,"",canInput);
	}
	
	@SuppressWarnings("unchecked")	
	public CodeGroupColumn(IModel headerModel, String propertyExpression, String sortProperty,ColumnType.CodeGroup codeGroupType,String codeGroupId,String conditionSql,boolean canInput) {
		super(headerModel, propertyExpression, sortProperty);
		this.codeGroupType = codeGroupType;
		this.codeGroupId = codeGroupId;
		this.conditionSql = conditionSql;
		this.canInput = canInput;
	}	

	@SuppressWarnings("unchecked")	
	public CodeGroupColumn(IModel headerModel, String propertyExpression,ColumnType.CodeGroup codeGroupType,String codeGroupId) {
		this(headerModel, propertyExpression,codeGroupType,codeGroupId,true);
	}
	
	@SuppressWarnings("unchecked")	
	public CodeGroupColumn(IModel headerModel, String propertyExpression,ColumnType.CodeGroup codeGroupType,String codeGroupId,String conditionSql) {
		this(headerModel, propertyExpression,codeGroupType,codeGroupId,true);
	}
	
	@SuppressWarnings("unchecked")	
	public CodeGroupColumn(IModel headerModel, String propertyExpression,ColumnType.CodeGroup codeGroupType,String codeGroupId,boolean canInput) {
		this(headerModel, propertyExpression,codeGroupType,codeGroupId,"",canInput);
	}
	
	@SuppressWarnings("unchecked")	
	public CodeGroupColumn(IModel headerModel, String propertyExpression,ColumnType.CodeGroup codeGroupType,String codeGroupId,String conditionSql,boolean canInput) {
		super(headerModel, propertyExpression);
		this.codeGroupType = codeGroupType;
		this.codeGroupId = codeGroupId;
		this.conditionSql = conditionSql;
		this.canInput = canInput;
	}	
	
	@SuppressWarnings("unchecked")
	@Override
	protected FormComponent newEditableComponent(String id, final IModel model, IModel rowModel, AbstractColumn column){
		beforeNewCodeGroup(model,rowModel);
		if (getCodeGroupType()==ColumnType.CodeGroup.TREE){
			return newTreeCodeGroup(id,model);
		} else{
			return newListCodeGroup(id,model); 
		}
	}
			
	@SuppressWarnings("unchecked")	
	protected TreeCodeGroup newTreeCodeGroup(String id, final IModel model){
		return new TreeCodeGroup(id, model,codeGroupId,conditionSql,canInput){
			private static final long serialVersionUID = 1L;
			@Override
			protected ChangedNotifyType getChangedNotifyType(){
				return CodeGroupColumn.this.getChangedNotifyType();
			}
			@Override
			protected void onNoPopup(AjaxRequestTarget target){
				CodeGroupColumn.this.onNoPopup(this,target);
			}			
			@Override
			protected void onItemChanged(TreeCodeGroup codeGroup,ListItem newValue){
				CodeGroupColumn.this.onItemChanged(codeGroup,newValue);
			}			
			@Override
			protected void onAjaxItemChanged(TreeCodeGroup codeGroup,ListItem newValue,AjaxRequestTarget target){
				CodeGroupColumn.this.onAjaxItemChanged(codeGroup,newValue,target);
			}
		};
	}
	
	@SuppressWarnings("unchecked")	
	protected ListCodeGroup newListCodeGroup(String id, final IModel model){
		return new ListCodeGroup(id, model,codeGroupId,conditionSql,canInput){
			private static final long serialVersionUID = 1L;
			@Override
			protected ChangedNotifyType getChangedNotifyType(){
				return CodeGroupColumn.this.getChangedNotifyType();
			}
			@Override
			protected void onNoPopup(AjaxRequestTarget target){
				CodeGroupColumn.this.onNoPopup(this,target);
			}			
			@Override
			protected void onItemChanged(ListCodeGroup codeGroup,ListItem newValue){
				CodeGroupColumn.this.onItemChanged(codeGroup,newValue);
			}			
			@Override
			protected void onAjaxItemChanged(ListCodeGroup codeGroup,ListItem newValue,AjaxRequestTarget target){
				CodeGroupColumn.this.onAjaxItemChanged(codeGroup,newValue,target);
			}
		};
	}
	
	public String getCodeGroupId() {
		return codeGroupId;
	}

	public void setCodeGroupId(String codeGroupId) {
		this.codeGroupId = codeGroupId;
	}

	public String getConditionSql() {
		return conditionSql;
	}

	public void setConditionSql(String conditionSql) {
		this.conditionSql = conditionSql;
	}

	public ChangedNotifyType getChangedNotifyType() {
		return changedNotifyType;
	}

	public void setChangedNotifyType(ChangedNotifyType changedNotifyType) {
		this.changedNotifyType = changedNotifyType;
	}	
	
	public ColumnType.CodeGroup getCodeGroupType() {
		return codeGroupType;
	}

	public void setCodeGroupType(ColumnType.CodeGroup codeGroupType) {
		this.codeGroupType = codeGroupType;
	}
	
	public boolean isCanInput() {
		return canInput;
	}

	public void setCanInput(boolean canInput) {
		this.canInput = canInput;
	}
	
	@SuppressWarnings("unchecked")
	protected void beforeNewCodeGroup(final IModel model, IModel rowModel){
	}
	
	protected void onNoPopup(FormComponent<?> component,AjaxRequestTarget target){
	}
	
	protected void onItemChanged(FormComponent<?> component,ListItem newValue){		
	}

	protected void onAjaxItemChanged(FormComponent<?> component,ListItem newValue,AjaxRequestTarget target){
	}
	
}
